package net.maku.commons.biza.handle;

import cn.hutool.core.date.DateUtil;
import net.maku.framework.common.constant.Constant;
import net.maku.framework.common.utils.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @Author wenyignying'
 * @Description:
 * @Date
 */
@Component
public class CreateSysNOHandler {

    @Autowired
    private RedisUtils redisUtils;

    public static final String JR_COUNTER_PREFIX = Constant.JR + ":%s";//JR:yyyyMMdd->1
    public static final String JR_GOODS_COUNTER_PREFIX = Constant.JR + ":GOODS_NO:%s";//JR:yyyyMMdd:1->1
    public static final String BOX_N_COUNTER_PREFIX = Constant.JRXH + ":%s";
    public static final String ORDER_N_COUNTER_PREFIX = Constant.JRINT + ":%s";

    /**
     * @param businessType: 业务类型
     * @return String
     * @author
     * @description 不同业务类型生成业务单据系统编号
     */
    public String createSysNo(int businessType, String receiptId) {
        String yearMonth = DateUtil.format(new Date(), "yyyyMMdd");
        String sysNo = "", key;
        Long counter;
        DecimalFormat decimalFormat = new DecimalFormat("0000");
        switch (businessType) {
            case 1://货编号
                if (StringUtils.isNotEmpty(receiptId)) {
                    key = String.format(JR_GOODS_COUNTER_PREFIX, receiptId);
                    counter = redisUtils.counter(key, 1L, 1L, TimeUnit.DAYS);
                    sysNo = Constant.JR + receiptId + "-" + counter;
                }
                break;
            case 2://收据单号
                key = String.format(JR_COUNTER_PREFIX, yearMonth);
                counter = redisUtils.counter(key, 1L, 1L, TimeUnit.DAYS);
                sysNo = Constant.JR + yearMonth + decimalFormat.format(counter);
                break;
            case 3://箱号
                key = String.format(BOX_N_COUNTER_PREFIX, yearMonth);
                counter = redisUtils.counter(key, 1L, 1L, TimeUnit.DAYS);
                sysNo = Constant.JRXH + yearMonth + decimalFormat.format(counter);
                break;
            case 4://订单号
                key = String.format(ORDER_N_COUNTER_PREFIX, yearMonth);
                counter = redisUtils.counter(key, 1L, 1L, TimeUnit.DAYS);
                sysNo = Constant.JRINT + yearMonth + decimalFormat.format(counter);
                break;
            case 5:
            default:
                break;
        }
        return sysNo;
    }

    /**
     * @param sysCode:   当前业务系统中最新的编号
     * @param yearMonth: 当前年月
     * @return String
     * @author
     * @description 生成业务单据系统编号
     */
    public String getSysNo(String sysCode, String yearMonth) {
        if (StringUtils.isBlank(sysCode)) {
            // 若数据库查询出来额为空，则代表当月还未有数据
            return yearMonth + String.format(Constant.FOUR_NUMBER, Constant.ONE);
        }
        // 取末四位数字进行增加编号
        String code = sysCode.substring(sysCode.length() - 4);
        int number = Integer.parseInt(code) + 1;
        // 补全四位数字
        return yearMonth + String.format(Constant.FOUR_NUMBER, number);
    }

}
